//
//  shopDetail.m
//  ShopView
//
//  Created by HJ on 2017/3/10.
//  Copyright © 2017年 HJ. All rights reserved.
//

#import "shopDetail.h"
#import "ShopNameTableViewCell.h"
#import "SectionTwoTableViewCell.h"
@interface shopDetail()<UITableViewDataSource,UITableViewDelegate,UIWebViewDelegate>
{
    CGFloat pic_Height;
    CGFloat title_Height;
}
@property (strong, nonatomic) UIView *contentView;
@property (strong, nonatomic) UIButton *popBtn;
@property (strong, nonatomic) UIButton *lastBtn;
@property (strong, nonatomic) UILabel *seleLabel;
@property (strong, nonatomic) UIView *sigView;
@property (assign, nonatomic) NSInteger index;
@end
@implementation shopDetail

- (instancetype)initWithFrame:(CGRect)frame
{
    if ([super initWithFrame:frame]) {
        self.backgroundColor=[UIColor blueColor];
        self.tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStyleGrouped];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        [self addSubview:self.tableView];
        self.index=1;
        [self creatSigment];
        [self.tableView registerNib:[UINib nibWithNibName:@"ShopNameTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"ShopNameTableViewCell"];
        
        [self.tableView registerNib:[UINib nibWithNibName:@"SectionTwoTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"SectionTwoTableViewCell"];
        
    }
    return self;
}

#pragma mark ------------ delegate
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            return 5;
            break;
        case 1:
            return 0.01;
            break;
        default:
            return 0;
            break;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    switch (section) {
        case 0:
             return 0.01;
            break;
        case 1:
             return 49;
            break;
        default:
            return 0;
            break;
    }
    //return 0;
}
#pragma mark -----去除表头悬停效果
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if(scrollView == self.tableView) {
        //CGFloat sectionHeaderHeight = 50;
        if (scrollView.contentOffset.y>=MainH/4-84) {
            
            NSNotification * notice = [NSNotification notificationWithName:@"123" object:@"nil" userInfo:nil];
            //发送消息
            [[NSNotificationCenter defaultCenter]postNotification:notice];
        } else if (scrollView.contentOffset.y<MainH/4-84) {
            
            NSNotification * notice = [NSNotification notificationWithName:@"321" object:@"have" userInfo:nil];
            //发送消息
            [[NSNotificationCenter defaultCenter]postNotification:notice];
        }
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section ==1){
        if (self.index==1) {
            return 125;
        }
        return 44;
    }else if (indexPath.section ==0&&indexPath.row ==0){
        //CGFloat width = (self.frame.size.width - 20 - 36)/4;
        return MainH/4;
    }else if (indexPath.section ==0&&indexPath.row ==1){
        CGFloat height =[self getHeightByWidth:self.frame.size.width-20 title:_titleString font:FontAndStyle14] + 200;
        return height;
    }
    else{
        return MainH;
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            return 2;
            break;
        case 1:
            if (self.index==1) {
                return 10;
            }
            return 4;
            break;
        default:
            return 1;
            break;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * hjID = [NSString stringWithFormat:@"%ldcell%ld",indexPath.section,indexPath.row];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    UITableViewCell * hjcell = [tableView dequeueReusableCellWithIdentifier:hjID];
    if (!hjcell) {
        hjcell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:hjID];
    }
    switch (indexPath.section) {
        case 0:{
            if (indexPath.row==1) {
                ShopNameTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ShopNameTableViewCell" forIndexPath:indexPath];
                cell.titleLabel.numberOfLines = 0;
                cell.titleLabel.text = self.titleString;
                cell.titleLabel.adjustsFontSizeToFitWidth =YES;
               // cell.moneyLabel.text = self.moneyString;
                NSMutableAttributedString *newPrice = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@",cell.deleteLabel.text]];
                [newPrice addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(0, newPrice.length)];
                cell.deleteLabel.attributedText = newPrice;
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                return cell;
            }else{
                UIImageView *imag=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, MainW, MainH/4)];
                imag.backgroundColor=[UIColor cyanColor];
                imag.image=[UIImage imageNamed:@"fo"];
                [hjcell addSubview:imag];
                hjcell.selectionStyle = UITableViewCellSelectionStyleNone;
                return hjcell;
            }
        }
            break;
        case 1:{
            // 视频
            if (self.index==1) {
                SectionTwoTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"SectionTwoTableViewCell" forIndexPath:indexPath];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                return cell;

            }else{
                hjcell.textLabel.text=@"888";

            }
                       hjcell.selectionStyle = UITableViewCellSelectionStyleNone;
            return hjcell;
        }
            break;
            
        default:
        {
            SectionTwoTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"SectionTwoTableViewCell" forIndexPath:indexPath];
            cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
            break;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.section) {
        case 2:
            NSLog(@"正品??");
            break;
            
        default:
            break;
    }
    
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section==1) {
        return self.sigView;
    }
    return nil;
}
#pragma mark ---加载选择项 防止重复加载
-(void)creatSigment{
    UIView*view=[[UIView alloc]init];
    view.backgroundColor=[UIColor cyanColor];
    for (int i=0; i<2; i++) {
        UIButton*tuButton=[[UIButton alloc]initWithFrame:CGRectMake(0+MainW/2*i, 12, MainW/2, 25)];
        [tuButton setTitle:@"图文详情" forState:UIControlStateNormal];
        [tuButton setTitle:@"选择图文详情" forState:UIControlStateSelected];
        if (i==0) {
            tuButton.selected=YES;
            self.lastBtn=tuButton;
        }
        tuButton.tag=i+100;
        [tuButton addTarget:self action:@selector(refreshLoadata:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:tuButton];
    }
    _seleLabel=[[UILabel alloc]initWithFrame:CGRectMake(MainW/8, 37, MainW/4, 1)];
    _seleLabel.backgroundColor=[UIColor redColor];
    [view addSubview:_seleLabel];
    self.sigView=view;
}
-(void)refreshLoadata:(UIButton *)sender{
    if (sender.tag==100) {
        self.seleLabel.frame=CGRectMake(MainW/8, 37, MainW/4, 1);
        self.index=1;
    }else{
       self.seleLabel.frame=CGRectMake(MainW*5/8, 37, MainW/4, 1);
        self.index=2;
    }
    if (sender!=self.lastBtn) {
        self.lastBtn.selected=NO;
        sender.selected=YES;
        self.lastBtn=sender;
    }else{
        self.lastBtn.selected = YES;
    }
    [self.tableView reloadData];
    
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (section==0) {
    UIView*view=[[UIView alloc]init];
    view.backgroundColor=[UIColor greenColor];
    return view;
    }else{
        UIView*view=[[UIView alloc]init];
        view.backgroundColor=[UIColor greenColor];
        return view;
    }
    return nil;
}
- (void)picBtnClick:(UIButton *)btn
{
    [self.delegate intoPicViewWith:btn.tag];
}

#pragma mark ----------- 点击视频
- (void)btnClick:(UIButton *)btn
{
    [self.delegate intoVideoViewWith:btn.tag];
}

- (CGFloat)getHeightByWidth:(CGFloat)width title:(NSString *)title font:(UIFont *)font
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, 0)];
    label.text = title;
    label.font = font;
    label.numberOfLines = 0;
    [label sizeToFit];
    CGFloat height = label.frame.size.height;
    return height;
}


@end
